define(['app'], function(app) {
	'use strict';

	//实例化方法
	var factory = function(Http) {
		function get_data_promotion(result) {
			if(HTTP_ISLOGIC) {
				result = '{"success": true,"msg": "","result": {"act_id": "001"},"session": "7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);

			} else {
				return result;
			}
		}

		function get_data_packets(result) {
			if(HTTP_ISLOGIC) {
				result = '{"success": true,"msg": "","result": {"rule": [{"id": "001"},{"id": "002"}]},"session": "7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else
				return result;
		}
		function get_data_goods_list(result) {
			if(HTTP_ISLOGIC) {
				result = '{"success":true,"msg":"","result":{"store_count":"500","goods":[{"goods_id":"001","name":"十字螺丝刀","introduce":"十字螺丝刀","price":10.5,"mktprice":11,"preprice":10,"store_id":"WuJin_1","store_name":"五金一店","image":{"id":1,"image":"img/upload/pro-img01.jpg"},"store":1234,"cat_type_name":"工具-小型工具-螺丝刀"},{"goods_id":"002","name":"一字螺丝刀","introduce":"一字螺丝刀","price":10.5,"mktprice":11,"preprice":10,"store_id":"WuJin_1","store_name":"五金一店","image":{"id":2,"image":"img/upload/pro-img01.jpg"},"store":1000,"cat_type_name":"工具-小型工具-螺丝刀"},{"goods_id":"003","name":"扳手","introduce":"好扳手","price":5.5,"mktprice":8,"preprice":5,"store_id":"WuJin_1","store_name":"五金一店","image":{"id":2,"image":"img/upload/pro-img01.jpg"},"store":1000,"cat_type_name":"工具-小型工具-螺丝刀"},{"goods_id":"004","name":"活动扳手","introduce":"好活动扳手","price":5.5,"mktprice":8,"preprice":5,"store_id":"WuJin_1","store_name":"五金一店","image":{"id":2,"image":"img/upload/pro-img01.jpg"},"store":1000,"cat_type_name":"工具-小型工具-螺丝刀"}],"page":[{"limit":8,"tPage":5,"cPage":1,"count":4}]},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else
				return result
		}
		function get_data_promotion_agenter(result){
			if(HTTP_ISLOGIC){
				result = '{"success":true,"msg":"","result":{"store":[{"store_id":"001","store_name":"世达","change_price":true,"region":[{"region_id":"001","region_name":"江南"},{"region_id":"002","region_name":"浙江"}],"goods":[{"goods_id":"002","goods_name":"一字螺丝刀","image":"img/upload/pro-img01.jpg","price":10.5,"promotion_price":10,"count":1000,"least_buy_num":10},{"goods_id":"001","goods_name":"十字螺丝刀","image":"img/upload/pro-img01.jpg","price":10.5,"promotion_price":10,"count":1000,"least_buy_num":10}]},{"store_id":"002","store_name":"益达","change_price":true,"region":[{"region_id":"003","region_name":"江苏"},{"region_id":"004","region_name":"广西"}],"goods":[{"goods_id":"001","goods_name":"一字螺丝刀","image":"img/upload/pro-img01.jpg","price":10.5,"promotion_price":10,"count":1000,"least_buy_num":10},{"goods_id":"002","goods_name":"十字螺丝刀","image":"img/upload/pro-img01.jpg","price":10.5,"promotion_price":10,"count":1000,"least_buy_num":10}]}]},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
                return JSON.parse(result);
			}else
			return result;
		}
		function get_data_promotion_detail(result){
			if(HTTP_ISLOGIC){
				result = '{"success":true,"msg":"","result":{"act_id":"001","name":"促销","start_time":"2017/01/01 09:00:00","end_time":"2017/02/01 09:00:00","create_group":true,"has_packets":true,"video_type":"1","image":[{"id":"001","image":"img/upload/pro-img01.jpg"},{"id":"002","image":"img/upload/pro-img01.jpg"}],"brand_id":"001","brand_name":"方法","cat_type":[{"id":"001","name":"手动工具"},{"id":"002","name":"电动工具"}]},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			}else
			return result;
		}
		function get_data_agenter_getlist(result){
			if(HTTP_ISLOGIC){
				result = '{"success":true,"msg":"","result":{"issue_type":"3","page":[{"limit":8,"tPage":5,"cPage":1,"count":3}],"store":[{"store_id":"001","store_name":"世达"},{"store_id":"003","store_name":"博士"},{"store_id":"004","store_name":"横达"}]},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			}else
			return result;
		}
		//可选品牌
		function get_brand_list(result) {
			if(HTTP_ISLOGIC) {
				result = '{"success":true,"msg":"","result":{"brand":[{"id":"001","name":"世达"},{"id":"002","name":"博士"}]},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else {
				return result;
			}
		}
		
		//返回信息
		return {
			//制造商发布修改促销活动
			set_promotion: function(act_id, name, start_time, end_time, create_group, has_packets, video_type, image, brand_id, cate_type,discount_type,discount_order_type,discount_total,discount_price,discount_percent,store,callback) {
				var updateParams = new Object();
				updateParams.act_id = act_id;
				updateParams.name = name;
				updateParams.start_time = start_time;
				updateParams.end_time = end_time;
				updateParams.create_group = create_group;
				updateParams.has_packets = has_packets;
				updateParams.video_type = video_type;
				updateParams.image = image;
				updateParams.brand_id = brand_id;
				updateParams.cate_type = cate_type;
				updateParams.discount_type = discount_type;
				updateParams.discount_order_type = discount_order_type;
				updateParams.discount_total = discount_total;
				updateParams.discount_price = discount_price;
				updateParams.discount_percent = discount_percent;
				updateParams.store = store;
				updateParams = JSON.stringify(updateParams);				
				//post请求
				Http.post({
						method: 'marketing.promotion.createedit',
						updateParams: updateParams

					},
					function(result) {
						//函数回调
						if(callback)
							callback(get_data_promotion(result));
					},
					function(e) {

					},
					function() {

					});
			},
			packets_edit: function(act_id, order_flg, advance, rule, callback) {
				var updateParams = new Object();
				updateParams.act_id = act_id;
				updateParams.order_flg = order_flg;
				updateParams.advance = advance;
				updateParams.rule = rule;
				updateParams = JSON.stringify(updateParams);		
				Http.post({
					method: 'marketing.packets.edit',
					updateParams: updateParams
				}, function(result) {
					if(callback)
						callback(get_data_packets(result));
				}, function(e) {

				}, function() {

				})
			},
			goods_list: function(cat_type, name, brand_id ,callback) {
				Http.post({
					method: 'goods.common.getlist',
					cat_type: cat_type,
					paglimit: 8,
					nPage: 1,
					show_count: 5,
					brand_id: brand_id,
					picSize: 'CS',
					name:name
				}, function(result) {
					if(callback)
					callback(get_data_goods_list(result).result);
				}, function(e) {

				}, function() {

				})
			},
			promotion_agenter:function(act_id,callback){
				Http.post({
					method:'marketing.promotion.agenter',
					act_id:act_id
				},function(result){
					if(callback)
					callback(get_data_promotion_agenter(result).result);
				},function(e){
					
				},function(){
					
				})
			},
			promotion_detail:function(act_id,callback){
				Http.post({
					method:'marketing.promotion.detail',
					act_id:act_id,
				},function(result){
					if(callback)
					callback(get_data_promotion_detail(result).result);
				},function(e){
					
				},function(){
					
				})
			},
			agenter_getlist:function(key,region_id,callback){
				Http.post({
					method:'marketing.agenter.getlist',
					pagelimit:8,
					nPage:1,
					show_count:5,
					key:key,
					region_id:region_id
				},function(result){
					if(callback)
					callback(get_data_agenter_getlist(result).result);
				},function(e){
					
				},function(){
					
				})
			},
			//可选品牌
			brand_list: function(callback) {
				Http.post({
					method: 'goods.brand.getlist'
				},
				function(result) {
					//请求成功
					if(callback)
						callback(get_brand_list(result).result);
				},
				function(e) {
					//请求异常
				},
				function() {
					//请求完成，一般不做什么事情
				});
			},
		};
	}

	factory.$inject = ['Http'];
	return app.registerService("D_add", factory);
});